package com.example.book_test.controller;


import com.example.book_test.config.MyAspect;
import com.example.book_test.service.BookService;

import com.example.book_test.model.*;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {
    @Autowired
    BookService bookService;

    @MyAspect
    @RequestMapping("/getBookListByPage")
    public Result getBookListByPage(PageRequest pageRequest, HttpSession session){
        log.info("查询翻页信息，pageRequest：{}", pageRequest);
        //用户登录检验
//        UserInfo userInfo = (UserInfo) session.getAttribute(Constants.SESSION_USER_KEY);
//        if (userInfo == null || userInfo.getId() <= 0 || "".equals(userInfo.getUserName())){
//            //用户未登录
//            return Result.unlogin();
//        }
//        if (pageRequest.getPageSize() < 0 || pageRequest.getCurrentPage() < 1){
//            return null;
//        }
        PageResult<BookInfo> res = null;
        try {
            res = bookService.selectBookInfoByPage(pageRequest);
            return Result.success(res);
        }catch (Exception e){
            log.error("查询翻页信息错误");
            return Result.fail(res, e.getMessage());
        }
    }

    @RequestMapping(value = "/addBook",produces = "application/json")
    public String addBook(BookInfo bookInfo){
        log.info("接收到添加图书请求，bookinfo:{}", bookInfo);
        if (bookInfo == null
                ||!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount() < 0
                || bookInfo.getPrice() == null
                || !StringUtils.hasLength(bookInfo.getPublish())){
            log.error("传参错误，bookInfo:{}", bookInfo);
            return "参数校验失败，请检查入参";
        }

        Integer res = bookService.addBook(bookInfo);
        if (res <= 0){
            log.error("添加图书出错：bookInfo:{}", bookInfo);
            return "添加图书出错，请联系管理员";
        }
        return "";
    }

    @RequestMapping("/queryBookInfoById")
    public BookInfo queryBookInfoById(Integer bookId){
        log.info("根据id查询图书，bookId:{}", bookId);

        try{
            BookInfo bookInfo = bookService.queryBookById(bookId);
            return bookInfo;
        }catch (Exception e){
            log.error("查询图书失败，e：{}", e);
        }

        return null;
    }

    @RequestMapping(value = "/updateBook", produces = "application/json")
    public String updateBook(BookInfo bookInfo){
        log.info("接收到的bookInfo：{}", bookInfo);

        Integer result = bookService.updateBook(bookInfo);
        if (result == 0){
            log.error("更新图书失败，请联系管理员");
            return "失败";
        }

        return "";
    }

    @RequestMapping(value = "/batchDelete", produces = "application/json")
    public String batchDeleteBook(@RequestParam List<Integer> ids){
        log.info("接收到的ids:{}", ids);
        Integer res = bookService.batchDeleteBook(ids);
        if (res <= 0){
            log.error("批量删除失败，ids:{}", ids);
            return "失败";
        }
        return "";
    }
}
